package com.yzkj.cpjxc.business.framework.utils;

import java.text.SimpleDateFormat;
import java.util.*;

public class IDUtils {
	private static Map<String, Object> map = Collections.synchronizedMap(new HashMap<String, Object>());
	private static String NUMBER = "number";
	private static String LASTSN = "LASTSN";

	public static synchronized String genID() {
		if (map.get(NUMBER) == null) {
			map.put(NUMBER, new Integer(0));
		}
		String id = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
		int number = ((Integer) map.get(NUMBER)).intValue();
		String no = "" + number;
		if (number == 9999) {
			number = 0;
		} else {
			number++;
		}
		map.put(NUMBER, new Integer(number));
		no = no.trim();
		int s = 4 - no.length();
		for (int i = 0; i < s; i++) {
			no = "0" + no;
		}
		id += no;
		return id;
	}

	public static synchronized String genID(String prefix) {
		Date dt = new Date();
		String sn = prefix;
		sn += new SimpleDateFormat("yy").format(dt);
		char ch = (char) (DateTimeUtils.getMonth(dt) + 64);
		sn += ch;
		if (DateTimeUtils.getDay(dt) > 26) {
			sn += DateTimeUtils.getDay(dt) - 26;
		} else {
			ch = (char) (DateTimeUtils.getDay(dt) + 64);
			sn += ch;
		}
		ch = (char) (DateTimeUtils.getHour(dt) + 64);
		sn += ch;
		String no = "" + (DateTimeUtils.getMinute(dt) * 60 + DateTimeUtils.getSecond());
		for (int i = 0; i < 4 - no.length(); i++) {
			no = "0" + no;
		}
		sn += no;
		Object obj = map.get(LASTSN);
		if (obj == null) {
			map.put(LASTSN, sn);
		} else {
			if (obj.equals(sn)) {
				try {
					IDUtils.class.wait(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				return IDUtils.genID(prefix);
			} else {
				map.put(LASTSN, sn);
			}
		}
		return sn;
	}

	public static String genNo(long value, int m) {
		Map<String, String> map = new LinkedHashMap<String, String>();
		for (int i = 1; i <= 36; i++) {
			if (i <= 9) {
				map.put(i + "", i + "");
			} else if (i == 10) {
				map.put(i + "", "0");
			} else {
				map.put(i + "", (char) (i + 55) + "");
			}
		}
		String no = "";
		while (value >= m) {
			no = map.get((value % m) + "") + no;
			value = value / m;
		}
		no = map.get(value + "") + no;
		return no;
	}

	public static synchronized String genUUID() {
		String key = UUID.randomUUID().toString();
		key = key.replace("-", "");
		return key.toUpperCase();
	}

	public static void main(String[] args) {
		System.out.println(genUUID());
	}

}
